<?php
// You can optionally specify the above values in a separate file.
if (file_exists(dirname($_SERVER["SCRIPT_FILENAME"]) . '/config.php')) include_once(dirname($_SERVER["SCRIPT_FILENAME"]) . '/config.php');

if (!defined('API_KEY'))
	{
	// Modify these values!
	define('API_KEY',	'____');

	define('HOST',		'localhost');
	define('USERNAME',	'____');
	define('PASSWORD',	'____');
	define('SCHEMA',	'____');
	define('TABLE',		'____');

	// Probably don't need to modify these
	define('TIMESTAMP',	'timestamp');
	define('DEVICE_KEY',	'device_key');
	define('DEVICE_LABEL',	'device_label');
	define('LATITUDE',	'latitude');
	define('LONGITUDE',	'longitude');
	define('ALTITUDE',	'altitude');
	define('SPEED',		'speed');
	define('HEADING',	'heading');

	// If the API version changes, this program may not work. Set the following to true if you don't care and want to run it any way
	define('OBEY_VERSION',	true);
	}

// The rest is just the guts. Prolly no need to goof with the following
$connection = mysql_pconnect(HOST, USERNAME, PASSWORD); if (!$connection) die("Couldn't connect to the database using the host/username/password combination specified.\n");

if (!mysql_select_db(SCHEMA, $connection)) die("Connected to the database server fine, but couldn't access the schema specified.\n");

if (!table_exists($connection))
	create_table($connection);

$data = file_get_contents("http://www.instamapper.com/api?action=getPositions&key=" . API_KEY . "&num=1000");

$data = explode("\n", $data);

if (trim($data[0]) != 'InstaMapper API v1.00')
	if (OBEY_VERSION) die("InstaMapper API version has changed. Giving up for now, but modify your settings if you think this doesn't matter.\n");

unset($data[0]);

foreach ($data as $marker)
	{
	if (empty($marker)) continue;
	
	list($device_key, $device_label, $timestamp, $latitude, $longitude, $altitude, $speed, $heading) = explode(',', $marker);
	
	$sql = "
	INSERT INTO `" . SCHEMA . "`.`" . TABLE . "`
		(
		`" . TIMESTAMP . "`,
		`" . DEVICE_KEY . "`,
		`" . DEVICE_LABEL . "`,
		`" . LATITUDE . "`,
		`" . LONGITUDE . "`,
		`" . ALTITUDE . "`,
		`" . SPEED . "`,
		`" . HEADING . "`
		)
	VALUES
		(
		 '" . date("Y-m-d H:i:s", $timestamp) . "',
		'" . mysql_real_escape_string($device_key) . "',
		'" . mysql_real_escape_string($device_label) . "',
		 " . mysql_real_escape_string($latitude) . ",
		 " . mysql_real_escape_string($longitude) . ",
		 " . mysql_real_escape_string($altitude) . ",
		 " . mysql_real_escape_string($speed) . ",
		 " . mysql_real_escape_string($heading) . "
		)";
	
	mysql_query($sql, $connection);
	
	if (mysql_errno($connection) === 0)
		{
		// Good.
		}
	else if (mysql_errno($connection) === 1062)
		{
		// Duplicate's are expected and ignored.
		}
	else
		{
		die("Something went wrong!\nThe error:\n" . mysql_error($connection) . "\n");
		}
	}

/*
$sql = "
insert into gps (latitude, longitude, altitude, bearing, speed)
values
	(
	'{$_REQUEST['latitude']}');
	'{$_REQUEST['longitude']}');
	'{$_REQUEST['altitude']}');
	'{$_REQUEST['bearing']}');
	'{$_REQUEST['speed']}'
	)";

mysql_query($sql);
*/

function table_exists($connection)
	{
	$sql = "SELECT `ORDINAL_POSITION` FROM `information_schema`.`COLUMNS` WHERE `TABLE_SCHEMA` = '" . SCHEMA . "' AND `TABLE_NAME` = '" . TABLE . "' and `COLUMN_NAME` IN ('" . DEVICE_KEY . "', '" . DEVICE_LABEL . "', '" . TIMESTAMP . "', '" . LATITUDE . "', '" . LONGITUDE . "', '" . ALTITUDE . "', '" . SPEED . "', '" . HEADING . "')";
	
	return (mysql_num_rows(mysql_query($sql, $connection)) == 8);
	}

function create_table($connection)
	{
	$sql = "
	CREATE TABLE `" . SCHEMA . "`.`" . TABLE . "` (
	`" . TIMESTAMP . "` timestamp NOT NULL,
	`" . DEVICE_KEY . "` char(12) NOT NULL,
	`" . DEVICE_LABEL . "` varchar(100) NOT NULL,
	`" . LATITUDE . "` double NOT NULL,
	`" . LONGITUDE . "` double NOT NULL,
	`" . ALTITUDE . "` float NOT NULL,
	`" . SPEED . "` float NOT NULL,
	`" . HEADING . "` int(11) NOT NULL,
	PRIMARY KEY (`" . TIMESTAMP . "`)
	)";
	
	mysql_query($sql, $connection) or die("Couldn't create the table!\nThe error:\n" . mysql_error($connection) . "\n");
	}
